package com.mark.maven.service.order.create.impl;

import com.alibaba.fastjson.JSONObject;
import com.mark.maven.dao.CreateOrderDao;
import com.mark.maven.domain.Order;
import com.mark.maven.service.order.create.CreateOrderService;
import com.mark.maven.service.order.create.req.CreateOrderRequest;
import com.mark.maven.service.order.create.resp.CreateOrderResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class CreateOrderServiceImpl implements CreateOrderService {

    private final CreateOrderDao createOrderDao;

    public CreateOrderServiceImpl(CreateOrderDao createOrderDao) {
        this.createOrderDao = createOrderDao;
    }

    @Override
    public CreateOrderResponse<String> createOrder(CreateOrderRequest request) {
        // toOrder
        Order order = toOrder(request);
        log.info("create order info :{}", JSONObject.toJSONString(order));
        // save order
        createOrderDao.saveOrder(order);
        // save customer, sender, receiver etc
        return CreateOrderResponse.ofSuccess(UUID.randomUUID().toString());
    }

    private Order toOrder(CreateOrderRequest request) {
        return Order.builder()
                .orderStatus("INIT")
                .createTime(new Date())
                .updateTime(new Date())
                .customer(request.getCustomer())
                .sender(request.getSender())
                .receiver(request.getReceiver())
                .build();
    }
}
